﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Text.RegularExpressions;

namespace TrainTicketsAuto.Helper
{
    public class Common
    {
        /// <summary>
        /// get thrain list
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string,string> GetThrainList()
        {
            
            Dictionary<string, string> dicThrainList=new Dictionary<string, string>();
            try
            {
                string QueryUrl = string.Format("http://dynamic.12306.cn/otsweb/js/common/station_name.js");
                HttpWebRequest request = (HttpWebRequest) WebRequest.Create(QueryUrl);
                request.ContentType = "application/x-www-form-urlencoded";
                request.Accept = "text/html, application/xhtml+xml, */*";
                request.Headers["Cache-Control"] = "no-cache";

                string afterQuery = HttpHelper.GetResponse(request, QueryUrl, "Get", "");

                string[] strArray = afterQuery.Replace("';", "").Split(new char[] { '@' });
                for (int i = 1; i < strArray.Length; i++)
                {
                    string[] strArray2 = strArray[i].Split(new char[] {'|'});
                    dicThrainList.Add(strArray2[1], strArray2[2]);
                }
                if (dicThrainList.Count == 0)
                {
                    WriteOutPutResult(InfoResource.message1);
                }
            }
            catch (Exception)
            {
                WriteOutPutResult(InfoResource.message2);
            }

            return dicThrainList;
        }

        /// <summary>
        /// Output Result
        /// </summary>
        /// <param name="text">result text</param>
        public static void WriteOutPutResult(string text)
        {
            MainForm.MainFormInstance.AssignOutputInformation(text);
        }
    }
}
